#region Copyright (c) 2006-2009 Widgetsphere LLC, All Rights Reserved
//--------------------------------------------------------------------- *
//                          Widgetsphere  LLC                           *
//             Copyright (c) 2006-2009 All Rights reserved              *
//                                                                      *
//                                                                      *
//This file and its contents are protected by United States and         *
//International copyright laws.  Unauthorized reproduction and/or       *
//distribution of all or any portion of the code contained herein       *
//is strictly prohibited and will result in severe civil and criminal   *
//penalties.  Any violations of this copyright will be prosecuted       *
//to the fullest extent possible under law.                             *
//                                                                      *
//THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
//TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
//TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
//CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
//THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF WIDGETSPHERE LLC   *
//                                                                      *
//UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
//PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
//SUBSTANTIALLY THE SAME, FUNCTIONALITY AS ANY WIDGETSPHERE PRODUCT.    *
//                                                                      *
//THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
//CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF WIDGETSPHERE,      *
//INC.  THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO       *
//INSURE ITS CONFIDENTIALITY.                                           *
//                                                                      *
//THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
//PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
//EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
//THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
//SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                      *
//THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
//--------------------------------------------------------------------- *
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using Widgetsphere.Generator.Common.GeneratorFramework;

namespace Widgetsphere.VsCodeTools
{
  public partial class StatisticsForm : Form
  {
    public StatisticsForm()
    {
      InitializeComponent();
      this.IsExpanded = false;
      lblHeader.Text = "These files were found in folders with generated files, however they were not part of the generation. They are most likely old files that need to be deleted. Please verify that you wish to remove these files.";
    }

    #region Class Members

    private bool _isExpanded = false;
		private List<ProjectItemGeneratedEventArgs> _generatedFileList = new List<ProjectItemGeneratedEventArgs>();

    #endregion

    #region Property Implementations

    public bool IsExpanded
    {
      get { return _isExpanded; }
      set
      {
        _isExpanded = value;

        if (this.IsExpanded)
        {
          //Expand
          this.FormBorderStyle = FormBorderStyle.Sizable;
          this.Size = new Size(this.MinimumSize.Width, this.MinimumSize.Height * 2);
          this.cmdDetails.Text = "<< Details";
        }
        else
        {
          //Shrink
          this.FormBorderStyle = FormBorderStyle.FixedDialog;
          this.Size = this.MinimumSize;
          this.cmdDetails.Text = "Details >>";
        }

      }
    }

    public string DisplayText
    {
      get { return txtStats.Text; }
      set { txtStats.Text = value; }
    }

		public List<ProjectItemGeneratedEventArgs> GeneratedFileList
    {
      get { return _generatedFileList; }
      set
      {
        try
        {
          _generatedFileList = value;
          if (_generatedFileList == null)
						_generatedFileList = new List<ProjectItemGeneratedEventArgs>();
          lstFile.Items.Clear();

          //Load the parent folders
          ArrayList folderList = new ArrayList();
					foreach (ProjectItemGeneratedEventArgs e in this.GeneratedFileList)
          {
						string fileName = e.FullName;
            if (fileName != "")
            {
              FileInfo fi = new FileInfo(fileName);
              if (!folderList.Contains(fi.DirectoryName))
                folderList.Add(fi.DirectoryName);
            }
          }

          //Now we have a folder list so load all files in all folders
          foreach (string folderName in folderList)
          {
            DirectoryInfo di = new DirectoryInfo(folderName);
            FileInfo[] fileList = di.GetFiles("*.*");
            foreach (FileInfo fi in fileList)
            {
              //Skip the projects
              if ((fi.Extension.ToLower() != ".csproj") &&
                (fi.Extension.ToLower() != ".scc") &&
                (fi.Extension.ToLower() != ".vssscc") &&
                (!fi.FullName.ToLower().Contains(".csproj.")))
              {
                lstFile.Items.Add(fi.FullName);
              }
            }
          }

          //Now loop and remove the items that were generated
					foreach (ProjectItemGeneratedEventArgs e in this.GeneratedFileList)
          {
						string fileName = e.FullName;
            if (fileName != "")
            {
              FileInfo fi = new FileInfo(fileName);
              lstFile.Items.Remove(fi.FullName);
            }
          }

        }
        catch (Exception ex)
        {
            throw ex;
        }
      }
    }

    #endregion

    #region Button Handlers

    private void cmdDetails_Click(object sender, EventArgs e)
    {      
      this.IsExpanded = !this.IsExpanded;      
    }

    private void cmdOK_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void cmdDelete_Click(object sender, EventArgs e)
    {
      if (MessageBox.Show("Do you wish to delete all checked files?", "Delete?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
      {
        try
        {
					SortedDictionary<string, ProjectItemGeneratedEventArgs> sortedList = new SortedDictionary<string, ProjectItemGeneratedEventArgs>();
					foreach(ProjectItemGeneratedEventArgs item in this.GeneratedFileList)
					{
						sortedList.Add(item.FullName, item);
					}

					List<int> indexes = new List<int>();
          for (int ii = 0; ii < lstFile.Items.Count; ii++)
          {
            if (this.lstFile.GetItemChecked(ii))
            {
							string fileName = (string)lstFile.Items[ii];
              indexes.Add(ii);

							ProjectItemGeneratedEventArgs genItem = null;
							if (sortedList.ContainsKey(fileName))
							{
								genItem = sortedList[fileName];
							}

              FileInfo fi = new FileInfo(fileName);
              fi.Attributes = FileAttributes.Normal;
              fi.Delete();
            }
          }

          //Remove the deleted file entry
          for (int jj = indexes.Count - 1; jj >= 0;jj-- )
            lstFile.Items.RemoveAt((int)indexes[jj]);

        }
        catch (Exception ex)
        {
            throw ex;
        }
      }
    }

    private void cmdCheckAll_Click(object sender, EventArgs e)
    {
      for (int ii = 0; ii < lstFile.Items.Count; ii++)
        lstFile.SetItemChecked(ii, true);
    }

    private void cmdUncheckAll_Click(object sender, EventArgs e)
    {
      for (int ii = 0; ii < lstFile.Items.Count; ii++)
        lstFile.SetItemChecked(ii, false);
    }

    #endregion

  }
}